﻿using System;
using System.IO;
using Cpuss.Strategies;
using NUnit.Framework;

namespace Cpuss.Tests
{

    /// <summary>
    /// Tests for Report.
    /// </summary>
    [TestFixture]
    public class HtmlReportTest
    {

        /// <summary>
        /// Check to see that the RepeatRunner used for the report is not null.
        /// </summary>
        [Test]
        public void RepeatRunnerNotNullTest()
        {
            HtmlReport actual = new HtmlReport(new RepeatRunner(10, 2, 1, new FirstComeFirstServed(), 5, 1), "Test", Directory.GetCurrentDirectory());

            Assert.IsNotNull(actual.RepeatRunner);
        }

        /// <summary>
        /// Check to see that the correct exception is raised when the RepeatRunner is null.
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void RepeatRunnerNullTest()
        {
            RepeatRunner repeatRunner = null;
            HtmlReport actual = new HtmlReport(repeatRunner, "Test", Directory.GetCurrentDirectory());
        }

        /// <summary>
        /// Check to see that the file name is correct.
        /// </summary>
        [Test]
        public void FileNameTest()
        {
            HtmlReport actual = new HtmlReport(new RepeatRunner(1, 2, 3, new FirstComeFirstServed(), 3, 1), "Test", Directory.GetCurrentDirectory());

            Assert.AreEqual("Test.html", actual.FileName);
        }

        /// <summary>
        /// Check to see that the correct exception is raised when the file name is null.
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void FileNameNullTest()
        {
            HtmlReport actual = new HtmlReport(new RepeatRunner(1, 2, 3, new FirstComeFirstServed(), 2, 1), null, Directory.GetCurrentDirectory());
        }

        /// <summary>
        /// Check to see that the correct exception is raised when the file name is of length 0.
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void FileNameLessThanOneCharInLengthTest()
        {
            HtmlReport actual = new HtmlReport(new RepeatRunner(1, 2, 3, new FirstComeFirstServed(), 3, 1), "", Directory.GetCurrentDirectory());
        }

        /// <summary>
        /// Check to see that the output dir is correct.
        /// </summary>
        [Test]
        public void OutputDirTest()
        {
            HtmlReport actual = new HtmlReport(new RepeatRunner(1, 2, 3, new FirstComeFirstServed(), 2, 1), "test", @"C:\");

            Assert.AreEqual(@"C:\", actual.OutputDirectory);
        }

        /// <summary>
        /// Check to see that the current directory is chosen if the outdir is not specified.
        /// </summary>
        [Test]
        public void OutputDirNullTest()
        {
            HtmlReport actual = new HtmlReport(new RepeatRunner(1, 2, 3, new FirstComeFirstServed(), 4, 1), "test", null);

            Assert.AreEqual(Directory.GetCurrentDirectory(), actual.OutputDirectory);
        }

        /// <summary>
        /// Check to see that the output dir specified is at least 3 chars in length, that is X:\
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void OutputDirProvidedNotValid()
        {
            HtmlReport actual = new HtmlReport(new RepeatRunner(1, 2, 34, new FirstComeFirstServed(), 4, 1), "test", "C:");
        }

        /// <summary>
        /// Check to see that the correct exception is thrown when the output dir provided doesn't exist.
        /// </summary>
        [Test]
        [ExpectedException(typeof(DirectoryNotFoundException))]
        public void OutputDirNotLocatedTest()
        {
            HtmlReport actual = new HtmlReport(new RepeatRunner(1, 2, 3, new FirstComeFirstServed(), 2, 23), "test", @"c:\nonexistant");
        }

        /// <summary>
        /// Check to see that the report file exists.
        /// </summary>
        [Test]
        public void CreateTest()
        {
            Guid id = Guid.NewGuid();
            HtmlReport actual = new HtmlReport(new RepeatRunner(10, 12, 8, new ShortestJobFirst(1), 5, 10), id.ToString(), Directory.GetCurrentDirectory());

            actual.Create();

            Assert.IsTrue(File.Exists(string.Format("{0}\\{1}.html", Directory.GetCurrentDirectory(), id)));
        }

        /// <summary>
        /// Check to see that the error message is correct.
        /// </summary>
        [Test]
        public void FileNullErrorMessageTest()
        {
            string actual = string.Empty;
            try
            {
                HtmlReport report = new HtmlReport(new RepeatRunner(1, 2, 3, new FirstComeFirstServed(), 3, 8), "", @"c:\");
            }
            catch (ArgumentException ex)
            {
                actual = ex.Message;
            }

            Assert.AreEqual("The name used for the generated report file must be at least one char in length.", actual);
        }

        /// <summary>
        /// Check to see that if the user provides a file extension then that extension is removed.
        /// Only HTML files are generated.
        /// </summary>
        [Test]
        public void FileWithExtensionTest()
        {
            HtmlReport actual = new HtmlReport(new RepeatRunner(1, 2, 3, new FirstComeFirstServed(), 3, 85), "Test.xml", Directory.GetCurrentDirectory());

            Assert.AreEqual("Test.html", actual.FileName);
        }

    }

}
